package com.bosssoft.g1.common.annotation;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import com.github.pagehelper.page.PageMethod;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;


/**
 * @author chen
 */
@Aspect
@Component
public class ApiLogAspect {

    @Pointcut("@annotation(com.bosssoft.g1.common.annotation.ApiLog)")
    public void apiLogPointcut() {
    }

    @Before("apiLogPointcut() && @annotation(apiLog)")
    public void beforeApiLog(JoinPoint joinPoint, ApiLog apiLog) {
        // 记录调用方法的开始日志
        System.out.println("Start method: " + joinPoint.getSignature().getName() + " with message: " + apiLog.value());
        if (apiLog.option() == ApiLogOptions.INCLUDE_PAGINATION) {
            // 启用分页功能
            // 示例分页设置，实际分页参数应动态获取
            PageMethod.startPage(1, 10);
        }
    }

    @AfterReturning(pointcut = "apiLogPointcut() && @annotation(apiLog)", returning = "result")
    public void afterReturningApiLog(JoinPoint joinPoint, ApiLog apiLog, Object result) {
        // 记录调用方法的结束日志
        System.out.println("End method: " + joinPoint.getSignature().getName() + " with message: " + apiLog.value());
        System.out.println("Method result: " + result);
    }
}
